// page/resell/resell.js

import { Shop } from '../../../models/shop'
import {check} from '../../../util/identityCheck'
import distribution from '../../../util/distribution'

const shop = new Shop()
const app = getApp()
var that
var list = []

Page({
  data: {
    list: [],
    category: [],
    checkIndex: 0,
    page: 0,
    showPopuop: false,
    count: 0,
    isIPhoneX: false,
    options: ['地区', '转让方式', '面积'],
    optionsChecked: ['', '', ''] ,
    showLoading: false,
    multiArray: [['全国', '北京', '安徽', '福建', '甘肃', '广东', '广西', '贵州', '海南', '河北', '河南', '黑龙江', '湖北', '湖南', '吉林', '江苏', '江西', '辽宁', '内蒙古', '宁夏', '青海', '山东', '山西', '陕西', '上海', '四川', '天津', '西藏', '新疆', '云南', '浙江', '重庆', '香港', '澳门', '台湾'], ['全国']],
    multiIndex:[0,0]
  },

  onReady () {
    that = this
    this.setData({
      isIPhoneX: app.globalData.isIPhoneX
    })
    shop.list({
    }, res => {
      this.setData({
        showLoading: res.length > 10
      })
      this.setData({
        list: res
      })
    })

    shop.category(res => {
      let category = []
      res.forEach(item => {
        item.value.unshift({
          cid: '',
          childName: '全部'
        })
        category.push(item)
      })
      this.setData({
        category
      })
    })
  },

  onLoad (options){
    distribution(options)
    if(!check())return
  },

  toNewPage (event) {
    let path = event.currentTarget.dataset.path
    let id = event.currentTarget.dataset.item.id
    wx.navigateTo({
      url: `/pages/${path}/${path}?id=${id}`
    })
  },

  onPullDownRefresh () {
    /**
     * 下拉刷新选项清空
     * **/
    this.setData({
      optionsChecked: ['', '', ''],
      page: 0
    })
    shop.list({
      page: 0
    }, res => {
      this.setData({
        list: res,
        showLoading: res.length > 10
      })
      wx.stopPullDownRefresh()
    })
  },

  bindLocationPickerChange (event) {
    // 地址选择
    let index = event.detail.value
    let Location = this.data.category[0].value[index]
    this.setData({
      'optionsChecked[0]': Location
    })
    this.search()
  },

  bindTypePickerChange (event) {
    // 转让方式
    let index = event.detail.value
    let Type = this.data.category[1].value[index]
    this.setData({
      'optionsChecked[1]': Type
    })
    this.search()
  },

  bindAreaPickerChange (event) {
    // 选择面积
    let index = event.detail.value
    let Area = this.data.category[2].value[index]
    this.setData({
      'optionsChecked[2]': Area
    })
    this.search()
  },

  onReachBottom () {
    this.setData({
      page: this.data.page+1
    })
    shop.list({
      transferSection: this.data.optionsChecked[0] && this.data.optionsChecked[0].cid || '',
      transferMode: this.data.optionsChecked[1] && this.data.optionsChecked[1].cid || '',
      acreage: this.data.optionsChecked[2] && this.data.optionsChecked[2].cid || '',
      page: this.data.page
    },res => {
      this.setData({
        list: [...this.data.list, ...res],
        showLoading: res.length > 10
      })
    })
  },

  search () {
    // 查找数据
    shop.list({
      transferArea: this.data.optionsChecked[0]|| '',
      transferMode: this.data.optionsChecked[1] && this.data.optionsChecked[1].cid || '',
      acreage: this.data.optionsChecked[2] && this.data.optionsChecked[2].cid || '',
      page: 0
    }, res => {
      this.setData({
        list: res,
        showLoading: res.length > 10
      })
      wx.stopPullDownRefresh()
    })
  },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      let timestamp = Date.parse(new Date())/1000
      return {
        path: `/pages/resell/resell?otherId=${wx.getStorageSync('userInfo').user_id}&time=${timestamp}&otherCode=${wx.getStorageSync('shareCode')}`
      }
    }
})


